package com.leyou.item.controller;

import com.leyou.common.vo.PageResult;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.sevice.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    /**
     * 分页条件查询  item/brand/page
     */
    @GetMapping("page")
    public ResponseEntity<PageResult<BrandDTO>> queryPage(
            @RequestParam(value = "page",defaultValue = "1") int page,
            @RequestParam(value = "rows",defaultValue = "5") int rows,
            @RequestParam(value = "sortBy",required = false) String sortBy,
            @RequestParam(value = "desc",required = false) boolean desc,
            @RequestParam(value = "search",required = false) String search
    ){
        return ResponseEntity.ok(brandService.queryPage(page,rows,sortBy,desc,search));
    }
    /**
     * 新增品牌
     */
    @PostMapping
    public ResponseEntity<Void> saveBrand(BrandDTO brandDTO, @RequestParam("cids") List<Long> cids ){
        brandService.saveBrand(brandDTO,cids);

        return ResponseEntity.status(HttpStatus.CREATED).build();
    }
    /**
     * 基于三级分类id查询品牌列表
     */
    @GetMapping("of/category")
    public ResponseEntity<List<BrandDTO>> queryByCategoryId(@RequestParam("id") Long id){
        return ResponseEntity.ok(brandService.queryByCategoryId(id));
    }
    /**
     * 基于品牌id查询品牌对象
     */
    @GetMapping("{id}")
    public ResponseEntity<BrandDTO> queryById(@PathVariable("id") Long id){
        return ResponseEntity.ok(brandService.queryById(id));
    }

    /**
     * 基于品牌id集合查询品牌数据
     */
    @GetMapping("queryBrandByIds")
    public ResponseEntity<List<BrandDTO>> queryBrandByIds(@RequestParam("ids") List<Long> ids){
        return ResponseEntity.ok(brandService.queryBrandByIds(ids));
    }


}
